package com.smm.web.vmc.util;

import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.AppMessage;
import com.gexin.rp.sdk.base.impl.ListMessage;
import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.NotificationTemplate;
import com.gexin.rp.sdk.template.style.Style0;
import com.smm.web.vmc.domain.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhenghao on 2016/10/13.
 *
 * 个推- 消息推送工具
 */
@Slf4j
@Service("gePushUtil")
public class GePushUtil implements PushUtilImpl {


    private static final String appId = "0ur16PNDcl6qZOe5SK1c85";
    private static final String appKey = "hifoyeabtT6JdzP0PmStQ2";
    private static final String masterSecret = "UpLbs4fEenAK7belCIFT76";
    private static final String url = "http://sdk.open.api.igexin.com/apiex.htm";


    /**
     * 推送信息给所有用户
     * @param title
     * @param content
     * @throws Exception
     */
    public void sendAll(String title,String content) throws Exception{

        IGtPush push = new IGtPush(url, appKey, masterSecret);

        //定义消息样式 通知栏消息布局样式(Style0 系统样式 Style1 个推样式 Style4 背景图样式 Style6 展开式通知样式)
        Style0 style = new Style0();
        style.setTitle(title);
        style.setText(content);

        // 定义"点击链接打开通知模板"，并设置标题、内容、链接
        NotificationTemplate template = new NotificationTemplate();
        template.setAppId(appId);
        template.setAppkey(appKey);
        template.setStyle(style);
        template.setTransmissionContent("");
        template.setTransmissionType(1);    //点击通知，启动应用

        List<String> appIds = new ArrayList<>();
        appIds.add(appId);

        // 定义"AppMessage"类型消息对象，
        AppMessage message = new AppMessage();
        message.setData(template);
        message.setAppIdList(appIds);
        message.setOffline(true);
        message.setOfflineExpireTime(1000 * 600);

        IPushResult ret = push.pushMessageToApp(message);

        log.info(ret.getResponse().toString());

    }


    /**
     * 推送信息给指定用户
     * @param title
     * @param content
     * @param user
     * @throws Exception
     */
    public void sendByUser(String title,String content,User user) throws Exception{

        //如果没有目标用户，直接返回
        if(user==null || StringUtils.isBlank(user.getClentid())) return;

        IGtPush push = new IGtPush(url, appKey, masterSecret);

        //定义消息样式
        Style0 style = new Style0();
        style.setTitle(title);
        style.setText(content);

        // 定义"点击链接打开通知模板"，并设置标题、内容、链接
        NotificationTemplate template = new NotificationTemplate();
        template.setAppId(appId);
        template.setAppkey(appKey);
        template.setStyle(style);
        template.setTransmissionContent("");
        template.setTransmissionType(1);    //点击通知，启动应用

        // 定义"AppMessage"类型消息对象，设置消息内容模板、发送的目标App列表、是否支持离线发送、以及离线消息有效期(单位毫秒)
        SingleMessage message = new SingleMessage();
        message.setData(template);
        message.setOfflineExpireTime(24 * 3600 * 1000);

        Target target = new Target();
        target.setAppId(appId);
        target.setClientId(user.getClentid());

        IPushResult ret = push.pushMessageToSingle(message,target);

        log.info(ret.getResponse().toString());

    }


    /**
     * 推送信息给指定用户列表
     * @param title
     * @param content
     * @param userList
     * @throws Exception
     */
    public void sendByUserList(String title,String content,List<User> userList) throws Exception{

        //如果没有目标用户，直接返回
        if(userList==null || userList.size()==0) return;

        IGtPush push = new IGtPush(url, appKey, masterSecret);

        //定义消息样式
        Style0 style = new Style0();
        style.setTitle(title);
        style.setText(content);

        // 定义点击通知打开应用模板，并设置标题、内容、链接
        NotificationTemplate template = new NotificationTemplate();
        template.setAppId(appId);
        template.setAppkey(appKey);
        template.setStyle(style);
        template.setTransmissionContent("");
        template.setTransmissionType(1);    //点击通知，启动应用


        ListMessage message = new ListMessage();
        message.setData(template);
        // 离线有效时间，单位为毫秒，可选
        message.setOfflineExpireTime(24 * 1000 * 3600);


        // 配置推送目标
        List<Target> targets = new ArrayList<>();

        for(User user : userList){

            Target target = new Target();
            target.setAppId(appId);
            target.setClientId(user.getClentid());
            targets.add(target);
        }

        // 获取taskID taskId用于在推送时去查找对应的message
        String taskId = push.getContentId(message);
        // 使用taskID对目标进行推送
        IPushResult ret = push.pushMessageToList(taskId, targets);

        log.info(ret.getResponse().toString());

    }



}
